package cn.chencaiju.day11;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

import org.junit.Test;

public class BufferedReaderTest {
	//字节流
	//is=InputStream();
	//字符流
	//fi=FileInputStream(is);
	//缓冲流
	//br=BufferedReader(fi);
//	String file="C:\\Users\\haiming\\Pictures\\wit.jpg";
	String file="read.txt";
	@Test public void testBufferedReader() {
//		System.out.println(bufferedReader(file,"UTF-8"));
//		System.out.println(bufferedReader(DataLoad.url,"gbk"));
		bufferedReader(DataLoad.url,"gbk");
	}
	
	/**
	 * 以指定的编码格式读取数据
	 * @param file 数据文件
	 * @param charsetName 编码格式
	 * @return
	 */
	public static String bufferedReader(String file,String charsetName) {
		
		try(
				BufferedReader br = new BufferedReader(
				new InputStreamReader(new FileInputStream(file),charsetName))
			)
		{
			StringBuilder sb=new StringBuilder();
			String temp=br.readLine();
			while(temp!=null&&!temp.isEmpty()) {
				sb.append(temp);
				sb.append("\n");
				temp=br.readLine();
			}
			System.out.println(Arrays.toString(sb.toString().split("[,\n]")));
//			System.out.println(sb.toString().split("[,\n]")[10]);
			return sb.toString();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
